package com.whopxx.biz.impl;

import com.whopxx.biz.RecommendBizService;
import com.whopxx.domain.dto.EquipmentDTO;
import com.whopxx.domain.dto.RecommendAddDTO;
import com.whopxx.domain.entity.EquipmentRecommend;
import com.whopxx.domain.entity.RecommendRecord;
import com.whopxx.service.EquipmentRecommendService;
import com.whopxx.service.RecommendRecordService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RecommendBizServiceImpl implements RecommendBizService {

    @Resource
    private RecommendRecordService recommendRecordService;

    @Resource
    private EquipmentRecommendService equipmentRecommendService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(RecommendAddDTO dto) {
        RecommendRecord record = new RecommendRecord();
        record.setHeroId(dto.getHeroId());
        recommendRecordService.save(record);
        List<EquipmentDTO> equipmentList = dto.getEquipmentList();
        List<EquipmentRecommend> recommends = equipmentList.stream().map(equipmentDTO -> {
            EquipmentRecommend recommend = new EquipmentRecommend();
            recommend.setEquipmentId(equipmentDTO.getEquipmentId());
            recommend.setStage(equipmentDTO.getStage());
            recommend.setRecommendRecordId(record.getId());
            return recommend;
        }).toList();
        return equipmentRecommendService.saveBatch(recommends);
    }
}
